package com.zmn.oms.admin.controller.conf.reply.content;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.InitialUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.model.entity.conf.reply.ReplyContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 类描述:回访内容修改控制器
 *
 * @author linfeng
 * @since 2019/10/21/14:47
 */
@Controller
@RequestMapping(value = "/conf/reply/content/")
public class ReplyContentModifyController extends ReplyContentBaseController {
    private Logger logger = LoggerFactory.getLogger(ReplyContentModifyController.class);

    @RequestMapping(value = "updateStatus")
    @ResponseBody
    //@RequiresPermissions("/conf/reply/content/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") int status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的回访内容！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        replyContentService.updateStatusBatch(arrayIds, status, getMcStaffRealName());

        return ResponseDTO.success(true,"操作成功！");
    }

    @RequestMapping(value = "updateSort")
    @ResponseBody
    //@RequiresPermissions("/conf/reply/content/list.action")
    public ResponseDTO updateSort(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer id, @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的回访内容！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置回访内容排序值！");
        }

        replyContentService.updateSortKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    @RequestMapping(value = "updateFirstLetter")
    @ResponseBody
    //@RequiresPermissions("/conf/reply/content/list.action")
    public ResponseDTO updateFirstLetter(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer id,
        @RequestParam(value = "firstLetter", defaultValue = "") String firstLetter) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改首字母的回访内容！");
        }
        if (firstLetter.isEmpty()) {
            return ResponseDTO.fail("请设置首字母！");
        }

        replyContentService.updateFirstLetterByKey(id, firstLetter, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    @RequestMapping(value = "addEdit")
    @ResponseBody
    //@RequiresPermissions("/conf/reply/content/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, HttpServletResponse response, @RequestBody ReplyContent replyContent) {
        Date now = DateUtil.getNow();

        Integer id = replyContent.getId();

        try {
            if (id == null) {
                replyContent.setCreater(getMcStaffRealName());
                replyContent.setCreateTime(now);
                replyContent.setStatus(GlobalConsts.YES);
                if (replyContent.getSort() == null) {
                    replyContent.setSort(1);
                }
                if (replyContent.getContent() != null) {
                    replyContent.setFirstLetter(InitialUtil.getFristEnName(replyContent.getContent()));
                }

                id = replyContentService.insert(replyContent);
            } else {
                ReplyContent result = replyContentService.findByKey(id);
                result.setSort(replyContent.getSort());
                result.setContent(replyContent.getContent());
                result.setUpdater(getMcStaffRealName());
                result.setUpdateTime(now);
                if (replyContent.getContent() != null) {
                    result.setFirstLetter(InitialUtil.getFristEnName(replyContent.getContent()));
                }
                result.setPlat(replyContent.getPlat());
                replyContentService.updateByKey(result);
            }

            return ResponseDTO.success(true,"操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("回访内容【{}】已经存在，不能重复添加！", replyContent.getContent());
            return ResponseDTO.fail("回访内容已经存在，不能重复添加！");
        } catch (Exception ex) {
            logger.error("[zmn]新增/修改回访内容失败,returnVisitContent:{},ex:{}", replyContent, ex.getMessage(), ex);
            return ResponseDTO.fail("操作失败！");
        }
    }
}
